﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction
{
	public UserGUI userGUI;
	public ArrowFactory arrowFactory;
	public FlyActionManager actionManager;
	public ScoreRecorder scoreRecorder;

	private List<GameObject> arrowQueue=new List<GameObject>();
	private GameObject target;
	private GameObject arrow;
	public GameObject bow;
	private string windStr = "";
	private float windX;
	private float windY;

	void Start()
	{
		SSDirector director = SSDirector.getInstance();
		director.currentSceneController = this;
		arrowFactory = Singleton<ArrowFactory>.Instance;
		actionManager = gameObject.AddComponent<FlyActionManager>() as FlyActionManager;
		scoreRecorder = Singleton<ScoreRecorder>.Instance;
		userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;
        
        CreateWind();
		LoadResources();
	}

	void Update()
    {
		for (int i = 0; i < arrowQueue.Count; i++)
		{
			GameObject temp = arrowQueue[i];
			if (temp.transform.position.z > 30 || arrowQueue.Count > 5)
			{
				arrowFactory.FreeArrow(arrowQueue[i]);
				arrowQueue.Remove(arrowQueue[i]);
			}
		}
	}

	public void LoadResources()
	{
		bow = Instantiate(Resources.Load("Prefabs/Bow", typeof(GameObject))) as GameObject;
		bow.transform.rotation = Quaternion.Euler(90f, 0.0f, 90f);
		target = Instantiate(Resources.Load("Prefabs/Target", typeof(GameObject))) as GameObject;
	}

    public void MoveBow(float offsetX, float offsetY)
    {
        if (bow.transform.position.x > 5)
        {
            bow.transform.position = new Vector3(5, bow.transform.position.y, bow.transform.position.z);
            return;
        }
        else if (bow.transform.position.x < -5)
        {
            bow.transform.position = new Vector3(-5, bow.transform.position.y, bow.transform.position.z);
            return;
        }
        else if (bow.transform.position.y < -3)
        {
            bow.transform.position = new Vector3(bow.transform.position.x, -3, bow.transform.position.z);
            return;
        }
        else if (bow.transform.position.y > 5)
        {
            bow.transform.position = new Vector3(bow.transform.position.x, 5, bow.transform.position.z);
            return;
        }
        
        offsetY *= Time.deltaTime;
        offsetX *= Time.deltaTime;
        bow.transform.Translate(0, -offsetX, 0);
        bow.transform.Translate(0, 0, -offsetY);
    }

    public void Shoot()
    {
        arrow = arrowFactory.GetArrow();
        arrowQueue.Add(arrow);

        Vector3 wind = new Vector3(windX, windY, 0);
        actionManager.ArrowFly(arrow, wind);
    }

    public int GetScore()
    {
        return scoreRecorder.score;
    }

    public string GetWind()
    {
        return windStr;
    }

    public void CreateWind()
    {
        windX = Random.Range(-2, 2);
        windY = Random.Range(-2, 2);
        string horizontal = "", vertical = "", level = "";
        if (windX > 0)
        {
            horizontal = "West";
        }
        else if (windX <= 0)
        {
            horizontal = "East";
        }
        if (windY > 0)
        {
            vertical = "South";
        }
        else if (windY <= 0)
        {
            vertical = "North";
        }
        if ((windX + windY) / 2 > -1 && (windX + windY) / 2 < 1)
        {
            level = "Level 1";
        }
        else if ((windX + windY) / 2 > -2 && (windX + windY) / 2 < 2)
        {
            level = "Level 2";
        }
        else if ((windX + windY) / 2 > -3 && (windX + windY) / 2 < 3)
        {
            level = "Level 3";
        }
        else
        {
            level = "Level 4";
        }

        windStr = vertical + horizontal + "Wind:" + " " + level;
    }

    public void NextArrow()
    {
        CreateWind();
    }

    public void Reset()
    {
        CreateWind();
        scoreRecorder.Reset();
        for (int i = 0; i < arrowQueue.Count; i++)
        {
            arrowFactory.FreeArrow(arrowQueue[i]);
        }
        arrowQueue.Clear();
    }
}